import {Util} from "@/utils/util.js"

export const AppConfig={
	baseUrl:"https://api.server.com",
	 // baseUrl:"http://192.168.3.9",
	// APP应用和后台通信的token
	token:"S3uzWltXZ40G9jSAGm4mmuNaLmWOCizW"
}
export class RequestParam{
	constructor(url,method,param,header) {
	    this.url = url
		this.method = method
		this.param = param
		this.header = header
	}
}
export const HttpRequest = (request) =>{
	uni.showLoading({title:"加载中..."});
	if(!request.header){
		request.header = {token:undefined,"session-key":Util.getCache("session-key")}
	}
	request.header.token=AppConfig.token;
	return new Promise((resolve,reject)=>{
		return uni.request({
			url: AppConfig.baseUrl + request.url,
			method:(request.method || "GET").toUpperCase(),
			header:request.header,
			data: request.param,
			success: (result) => {
				result = result.data;
				if(result.errno===103){
					uni.navigateTo({url:"/pages/login/login"});
					reject(result);
				}else if(result.errno===404){
					uni.showModal({title:'404错误',content: result.errmsg});
					reject(result);
				}else if(result.errno !== 200){
					uni.showModal({title:'错误',content: result.errmsg});
					reject(result);
				}
				resolve(result.data);
			},
			fail: (err) => {
				uni.showModal({title:'错误',content: err.errMsg});
				reject(err);
			},
			complete: () => {
				uni.hideLoading();
			}
		})
	})
}